package com.wmm.servlet;

import com.wmm.bean.PageBean;
import com.wmm.bean.Student;
import com.wmm.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "QueryStudentServlet" ,urlPatterns = "/queryStudent")
public class QueryStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String bystudentno = request.getParameter("bystunumber");
        String byname = request.getParameter("byname");
        System.out.println("bystudentno :" + bystudentno);
        System.out.println("byname : " + byname);
        System.out.println(byname == "");
        System.out.println(bystudentno == "");
        if( "" == byname ) {
            byname = null;
        }
        if("" == bystudentno) {
            bystudentno = null;
        }

        //2. 调用service查询
        StudentService service = new StudentService();
        PageBean<Student> queryList = service.findQueryStudentByPage("1","5",bystudentno,byname);
        //3. 将PageBean存入session
        HttpSession session = request.getSession();

        session.setAttribute("bystudentno",bystudentno);
        session.setAttribute("byname",byname);
        session.setAttribute("queryList",queryList);
        System.out.println(queryList.toString());
        //4. 转发到show.jsp
        request.getRequestDispatcher("show.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
